package com.vf.admin.client.controller;

import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.dto.AppBannerDto;
import com.vf.admin.client.service.AgentUserService;
import com.vf.admin.client.service.AppBannerService;
import com.vf.admin.common.Constants;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.miaodong.entity.AppBanner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/**
 * 轮播图
 */
@Controller
@RequestMapping("/app/banner")
public class AppBannerController extends ClientBaseController {
    @Autowired
    AppBannerService appBannerService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    AgentUserService agentUserService;

    /**
     * 获取banner图
     *
     * @param state 轮播图位置  轮播图位置：1直播  2首页
     * @return
     */
    @RequestMapping("/getBanner")
    @ResponseBody
    public Result getbanner(Integer state) {
        try {
            if (state == null || state != Constants.lunbotu_weizhi_sy && state != Constants.lunbotu_weizhi_zb) {
                return errorJSON("Carousel picture cannot be empty!");
            }
            List<AppBanner> bannerList = null;
            boolean exists = redisUtil.hasKey(RedisKeys.CACHE_LUNBOTU+state);
            if (exists) {
                bannerList = (List<AppBanner>) redisUtil.get(RedisKeys.CACHE_LUNBOTU+state);
            } else {
                bannerList = appBannerService.list(state);
                redisUtil.set(RedisKeys.CACHE_LUNBOTU+state, bannerList, Constants.lunbotu_time);
            }

            List<AppBannerDto> bannerDtoList = new ArrayList<>();
            if (bannerList != null && bannerList.size() > 0) {
                for (AppBanner appBanner : bannerList) {
                    if (appBanner.getStatus().intValue() == 2) {
                        continue;
                    }
                    AppBannerDto appBannerDto = new AppBannerDto();
                    appBannerDto.setId(appBanner.getId());
                    appBannerDto.setTitle(appBanner.getTitle());//轮播标题
                    appBannerDto.setLeixing(appBanner.getLeixing());//跳转类型 1、 跳转房间  2外网  3用户
                    appBannerDto.setImage(appBanner.getImage()+"?x-oss-process=image/resize,w_700/format,jpg/quality,q_85");//图片
                    appBannerDto.setUrladdress(appBanner.getUrladdress());//跳转地址
                    appBannerDto.setWeizhi(appBanner.getWeizhi());//轮播图位置：1直播  2首页
                    bannerDtoList.add(appBannerDto);
                }
            }
            return this.sucessJSON(bannerDtoList);
        } catch (Exception e) {
            return this.errorJSON("The system is busy, please try later!");
        }
    }

    @RequestMapping("/test")
    @ResponseBody
    public void test() {
        agentUserService.shareMoneyRecharge(58, new BigDecimal("850"));
    }
}
